package com.jacky;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.jacky.path_provider_ex.R;

public class JAlertDialog {

    private CharSequence title;
    private CharSequence content, btnOk, btnCancel;
    private DialogInterface.OnClickListener mOkListener, mCancelListener;
    private DialogInterface.OnDismissListener mOnDismissListener;
    private View.OnClickListener mClickListener;
    private boolean dialogCancelable = true, autoDismiss = true, isSingleMode = false,hasLinkClick = false;
    private Boolean isContentCenter = null;
    private int windowType;

    private Dialog dialog;

    public JAlertDialog setTitle(CharSequence title) {
        this.title = title;
        return this;
    }

    public JAlertDialog setMessage(CharSequence content) {
        this.content = content;
        return this;
    }

    public JAlertDialog setBtnCancel(CharSequence btnCancel, DialogInterface.OnClickListener listener) {
        this.btnCancel = btnCancel;
        this.mCancelListener = listener;
        return this;
    }

    public JAlertDialog setBtnOk(CharSequence btnOk, DialogInterface.OnClickListener listener) {
        this.btnOk = btnOk;
        this.mOkListener = listener;
        return this;
    }

    public JAlertDialog setClick(View.OnClickListener clickListener) {
        this.mClickListener = clickListener;
        return this;
    }

    public JAlertDialog setCancelable(boolean cancel) {
        this.dialogCancelable = cancel;
        return this;
    }

    public JAlertDialog setAutoDismiss(boolean autoDismiss) {
        this.autoDismiss = autoDismiss;
        return this;
    }

    public JAlertDialog setContentCenter(boolean contentCenter){
        this.isContentCenter = contentCenter;
        return this;
    }

    public JAlertDialog setHasLinkClick(boolean hasLinkClick){
        this.hasLinkClick = hasLinkClick;
        return this;
    }

    public JAlertDialog setSingleBtn(CharSequence btnText, DialogInterface.OnClickListener listener) {
        isSingleMode = true;
        btnOk = btnText;
        mOkListener = listener;
        return this;
    }

    public JAlertDialog setWindowType(int windowType){
        this.windowType =  windowType;
        return this;
    }

    public JAlertDialog setOnDismissListener(DialogInterface.OnDismissListener onDismissListener){
        this.mOnDismissListener = onDismissListener;
        return this;
    }

    public void show(Context context) {
        if(context == null || (context instanceof Activity && ((Activity) context).isFinishing())) return;

        View view = LayoutInflater.from(context).inflate(R.layout.j_alert_dialog, null, false);
        if(mClickListener != null)view.setOnClickListener(mClickListener);
        TextView titleView = view.findViewById(R.id.title);
        titleView.setText(title);
        TextView contentView = view.findViewById(R.id.tv_content);
        contentView.setText(content);
        if(TextUtils.isEmpty(title)){
            titleView.setVisibility(View.GONE);
        } //            contentView.setTextColor(context.getResources().getColor(R.color.text_66));

        if(hasLinkClick){
            contentView.setMovementMethod(LinkMovementMethod.getInstance());
        }
        if (isContentCenter != null){
            contentView.setGravity(isContentCenter ? Gravity.CENTER : Gravity.START);
        }

        TextView btnOkView = view.findViewById(R.id.btnOk);
        TextView btnCancelView = view.findViewById(R.id.btnCancel);
        TextView btnSingleView = view.findViewById(R.id.btn_single);
        if(isSingleMode) {
            if (isContentCenter == null)contentView.setGravity(Gravity.CENTER);
            btnSingleView.setText(TextUtils.isEmpty(btnOk) ? "确定" : btnOk);
            btnSingleView.setOnClickListener((v)->{
                dismiss();
                if(mOkListener != null) {
                    mOkListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                }
            });
            ((View)btnOkView.getParent()).setVisibility(View.GONE);
        } else {
            btnOkView.setText(TextUtils.isEmpty(btnOk) ? "确定" : btnOk);
            btnOkView.setOnClickListener((v)->{
                dismiss();
                if(mOkListener != null) {
                    mOkListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                }
            });

            btnCancelView.setText(TextUtils.isEmpty(btnCancel) ? "取消" : btnCancel);
            btnCancelView.setOnClickListener((v)->{
                dismiss();
                if(mCancelListener != null) {
                    mCancelListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                }
            });
            ((View)btnSingleView.getParent()).setVisibility(View.GONE);
        }

        if(context instanceof Activity) {
            dialog = new Dialog(context,R.style.DialogTransparent);
            dialog.setContentView(view);
        } else {
//            if(!AppUtils.canDrawOverlays(context)){
//                Logger.d("alert dialog,no permission for pop-up, need pop content ",content);
//                ToastUtil.showMsg("没有悬浮弹窗权限，无法弹窗");
//                return;
//            }
            dialog = new Dialog(context,R.style.DialogTransparent);
            dialog.setContentView(view);
        }
        dialog.setCancelable(dialogCancelable);
        dialog.setCanceledOnTouchOutside(dialogCancelable);
        if (mOnDismissListener != null)dialog.setOnDismissListener(mOnDismissListener);
        Window window = dialog.getWindow();
        if(window != null){
            if(windowType != 0)window.setType(windowType);
        }
        dialog.show();
    }

    public View getView(){
        return dialog == null || dialog.getWindow() == null ? null : dialog.getWindow().getDecorView();
    }

    public void dismiss() {
        if(!autoDismiss) return;
        if(dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public boolean isShowing(){
        return dialog != null && dialog.isShowing();
    }
}
